package com.link_with_health.controller;

import com.link_with_health.common.Result;
import com.link_with_health.common.TokenMap;
import com.link_with_health.exception.AuthenticationFailedException;
import com.link_with_health.model.AdminLogEntity;
import com.link_with_health.model.AdminUserEntity;
import com.link_with_health.model.UserEntity;
import com.link_with_health.service.AdminService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;


@Controller
@RequestMapping("/api/admin")
public class AdminController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    private final String AUTH_TOKEN = "Authorization";
    private TokenMap tokenMap = TokenMap.getInstance();

    @Resource(name = "adminService")
    AdminService adminService;


    /**
     * 通过token获取管理员信息
     * @param request
     * @return
     */
    @GetMapping("/accessAdmin")
    @ResponseBody
    public Result accessAdmin(HttpServletRequest request){
        String msg = "验签校验失败 无权限";
        String token = request.getHeader(AUTH_TOKEN);

        if(token == null || !(
                tokenMap.get(token)
                        instanceof AdminUserEntity)){
            logger.error("[verify user token failed]");
            return Result.unAuth(msg,msg);
        }

        try{
            AdminUserEntity entity = (AdminUserEntity) tokenMap
                    .get(token);

            entity = adminService
                    .getAdminUserById(entity.getAdminID());
            tokenMap.put(token,entity);
            //屏蔽密码
            entity.setAdminPW("*******");
            return Result.success(entity);

        } catch(Exception e){
            logger.error("[access adminUser failed in tokenMap]");
            msg="获取用户数据失败";
        }

        return Result.fail(msg);

    }

    @PostMapping("/updatePass")
    @ResponseBody
    public Result updatePass(@RequestBody Map<String,Object> json
            , HttpServletRequest request){
        String msg = "请求出错";
        String token = request.getHeader(AUTH_TOKEN);
            if(token==null)
                return Result.unAuth(msg,msg);

        String password = (String)json.get("password");
        String currentPass = (String)json.get("currentPass");
        try{

            AdminUserEntity entity = (AdminUserEntity)tokenMap.get(token);
            entity = adminService.getAdminUserById(entity.getAdminID());

            if(!password.equals(entity.getAdminPW())){
                msg = "密码不匹配";
                logger.info("[password missing match]");
                throw new Exception("[password missing match]");
            }


            entity.setAdminPW(currentPass);

            adminService.changeAdminUser(entity);
            tokenMap.put(token,entity);

            return Result.success(entity);
        } catch (Exception e) {
            logger.error("[failed to update ]");
            msg="修改失败";
        }

        return Result.fail(msg);
    }


    @PostMapping("/selectAdminById")
    @ResponseBody
    public Result selectAdminById(@RequestBody Map<String,Object> json
            , HttpServletRequest request){
        String token = request.getHeader(AUTH_TOKEN);

        System.out.println(tokenMap.containsKey(token));

        try{
            Integer adminId = (Integer)json.get("adminID");


        } catch (Exception e) {
            logger.error("");
        }

        return Result.fail("");
    }
}
